﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using pdtransport_v2._0.Models;
using System.IO;

namespace pdtransport_v2._0.Areas.back.Controllers
{
    public class xe
    {
        public Xe _xe;
        public List<Hinh> _hinhxe;
    }

    [Authorize(Roles="admin")]
    public class XeController : Controller
    {
        //
        // GET: /back/Xe/

        pdtransportDataContext datacontext = new pdtransportDataContext();

        public ActionResult DanhSach(string keyword)
        {
            int total_rows = 0;
            List<Xe> xes;
            if (keyword == null)
            {
                total_rows = (from _xe in datacontext.Xes
                              select _xe).Count();
            }
            else
            {
                total_rows = (from _xe in datacontext.Xes
                              where _xe.TenXe1.TenXe1.Contains(keyword) == true
                              select _xe).Count();
            }

            ViewBag.total_row = total_rows;
            string base_URL = "DanhSach"; // tên method của controller xeController

            // phan trang
            string URL_segment;
            try
            {
                URL_segment = Request.Url.Segments[4];
            }
            catch (Exception)
            {
                URL_segment = null;
            }
       
            Libraries.Pagination pagination = new Libraries.Pagination(base_URL, URL_segment, total_rows);
            string pageLinks = pagination.GetPageLinks();
            int start = (pagination.CurPage - 1) * pagination.PerPage;
            int offset = pagination.PerPage;
            if (URL_segment != null)
                pageLinks = pageLinks.Replace(base_URL + "/", "");
            ViewBag.pageLinks = pageLinks;
            // phan trang

            if (keyword == null)
            {
                xes = (from _xe in datacontext.Xes
                       select _xe).Skip(start).Take(offset).ToList();
            }
            else
            {
               
                xes = (from _xe in datacontext.Xes
                       where _xe.TenXe1.TenXe1.Contains(keyword) == true
                       select _xe).Skip(start).Take(offset).ToList();
               
            }

            ViewBag.total_rows = total_rows;
            return View(xes);
        }

        [HttpGet]
        public ActionResult Sua(int idxe)
        {
            Xe xe = (from x in datacontext.Xes
                     where x.IDXe == idxe
                     select x).Single();
            List<Hinh> hinh = (from h in datacontext.Hinhs
                               where h.TenXe == xe.TenXe
                               select h).ToList();
            xe _x = new xe();
            _x._xe = xe;
            _x._hinhxe = hinh;
            ViewBag.xe = _x;
            ViewBag.mien = (from mien in datacontext.Miens
                            where mien.TinhTrang == 1
                            select mien);
            return View();
        }

        [HttpPost]
        public ActionResult Sua(int idxe, int itenxe, string tenxe, string bks, int mien, int tinhtrang, IEnumerable<HttpPostedFileBase> files, string txtCheckList)
        {

            // thay đổi thông tin của Xe
            Xe xe = (from x in datacontext.Xes
                     where x.IDXe == idxe
                     select x).Single();

            xe.TinhTrang = tinhtrang;
            xe.Mien = mien;
            xe.BKS = bks;
            xe.TenXe = itenxe;
            datacontext.SubmitChanges();


            // Thể hiện lại kết quả thành công
            Xe _xe = (from x in datacontext.Xes
                     where x.IDXe == idxe
                     select x).Single();
            List<Hinh> _hinh = (from h in datacontext.Hinhs
                               where h.TenXe == xe.TenXe
                               select h).ToList();
            xe _x = new xe();
            _x._xe = _xe;
            _x._hinhxe = _hinh;
            ViewBag.xe = _x;
            ViewBag.lhdv = (from _lhdv in datacontext.LoaiHinhDichVus
                            where _lhdv.TinhTrang == 1
                            select _lhdv);

            ViewBag.mien = (from _mien in datacontext.Miens
                            where _mien.TinhTrang == 1
                            select _mien);

            ViewBag.loaixe = (from _loaixe in datacontext.LoaiXes
                              where _loaixe.TinhTrang == 1
                              select _loaixe);
            ViewBag.Sua = 1;
            return View();
        }

        public ActionResult Xoa(int idxe)
        {
            Xe _xe = (from x in datacontext.Xes
                      where x.IDXe == idxe
                      select x).Single();

            _xe.TinhTrang = 2;
            datacontext.SubmitChanges();

            ViewBag.Xoa = 1;
            return RedirectToAction("Danhsach");
        }

        [HttpGet]
        public ActionResult Them()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Them(int itenxe, string bks, int mien)
        {
            Xe xe = new Xe();

            xe.BKS = bks;
            xe.Mien = mien;
            xe.TenXe = itenxe;
            xe.TinhTrang = 1;
            datacontext.Xes.InsertOnSubmit(xe);
            datacontext.SubmitChanges();

            return RedirectToAction("Sua", new { idxe = xe.IDXe });
        }
    }
}
